<template>
  <div id="postFavoritePage">
    <!-- 搜索表单 -->
    <a-form layout="inline" :model="searchParams" @finish="doSearch" class="search-form">
      <a-form-item label="帖子标题">
        <a-input v-model:value="searchParams.title" placeholder="输入帖子标题" />
      </a-form-item>
<!--      <a-form-item label="用户 ID">-->
<!--        <a-input v-model:value="searchParams.userId" placeholder="输入用户 ID" />-->
<!--      </a-form-item>-->
      <a-form-item label="话题名称">
        <a-input v-model:value="searchParams.topicName" placeholder="输入话题名称" />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" html-type="submit">搜索</a-button>
      </a-form-item>
    </a-form>

    <!-- 收藏帖子列表 -->
    <div class="post-list">
      <a-card v-for="(record, index) in dataList" :key="record.id" class="post-card">
        <template #title>
          <span>{{ record.title }}</span>
          <a-button danger size="small" @click="doDelete(record.id)" style="float: right"
            >删除</a-button
          >
        </template>
<!--        <p>用户 : {{ record.userName }}</p>-->
        <p>话题名称: {{ record.topicName }}</p>
        <p>帖子内容: {{ record.content }}</p>
        <p>创建时间: {{ dayjs(record.createTime).format('YYYY-MM-DD HH:mm:ss') }}</p>
      </a-card>
    </div>

    <!-- 分页 -->
    <a-pagination
      :current="searchParams.current"
      :page-size="searchParams.pageSize"
      :total="total"
      @change="handlePaginationChange"
      class="pagination"
      show-size-changer
      :show-total="(total) => `共 ${total} 条`"
    />
  </div>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue'
import { message } from 'ant-design-vue'
import dayjs from 'dayjs'
import {
  deletePostUsingPost,
  listFavoritePostVoByPageUsingPost,
} from '@/api/favoritePostController'

// 数据源
const dataList = ref([])
const total = ref(0)

// 搜索条件
const searchParams = reactive({
  current: 1,
  pageSize: 10,
  sortField: 'createTime',
  title: '',
  userId: '',
  topicName: '',
})

// 处理分页变化
const handlePaginationChange = (page: number, pageSize: number) => {
  searchParams.current = page
  searchParams.pageSize = pageSize
  fetchData()
}

// 获取数据
const fetchData = async () => {
  const res = await listFavoritePostVoByPageUsingPost({ ...searchParams })
  if (res.data.data) {
    dataList.value = res.data.data.records ?? []
    total.value = res.data.data.total ?? 0
    // message.success('获取数据成功')
  } else {
    message.error('获取收藏帖子数据失败'+res.data.message)
  }
}

// 执行搜索
const doSearch = () => {
  searchParams.current = 1
  fetchData()
}

// 删除数据
const doDelete = async (id: string) => {
  if (!id) return
  const res = await deletePostUsingPost({ id })
  if (res.data.data) {
    message.success('删除收藏成功')
    fetchData()
  } else {
    message.error('删除收藏失败')
    fetchData()

  }
}

onMounted(() => {
  fetchData()
})
</script>

<style scoped>
#postFavoritePage {
  padding: 20px;
}

.search-form {
  margin-bottom: 20px;
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.post-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 20px;
}

.post-card {
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  border-radius: 8px;
  overflow: hidden;
}

.post-card p {
  margin: 8px 0;
}

.pagination {
  margin-top: 20px;
  text-align: center;
}
</style>
